package com.mediacloud.app.newsmodule.fragment.recommend;

import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.mediacloud.app.assembly.util.FunKt;
import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.assembly.views.NetImageViewX;
import android.widget.TextView;
import com.mediacloud.app.model.component.ComponentItem;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.ArticleListData;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.model.news.PayTipsUtilsKt;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapter;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yoake on 2017/9/30.
 */

public class ComponentLiveLookBackView extends BaseComponentRelatedView implements AdapterView.OnItemClickListener, BaseRecyclerAdapter.ItemClickListener {
    //    private EmbedGridView mGridView;
    private RecommendComponentAdapter mAdapter;
    private ArrayList<CatalogItem> mCatalogItem;
    private RecyclerView mRecyclerView;
    private static final int STYLE_HORIZONTAL = 1013;
    private static final int STYLE_GRID = 1007;

    private int style;

    public ComponentLiveLookBackView(Context context, int style) {
        super(context);
        this.style = style;
        initManagerStyle();
    }

    @Override
    public int setLayoutRes() {
        return R.layout.component_related_column;
    }



    @Override
    public void initOtherViews() {
        mRecyclerView = Utility.findViewById(mRootView, R.id.recycler_view_recommended_column_component);
        mAdapter = new RecommendComponentAdapter(getContext());
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setItemClickListener(this);
    }

    private void initManagerStyle() {
        if (style == STYLE_GRID) {
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mRootView.getContext(), 3);
            gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
            mRecyclerView.setLayoutManager(gridLayoutManager);
        } else {
            mRecyclerView.setPadding((int) getResources().getDimension(R.dimen.dimen6), 0, 0, 0);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mRootView.getContext());
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mRecyclerView.setLayoutManager(linearLayoutManager);
        }
    }


    @Override
    public void onDataSuccess(ArticleListData data) {
        if (data.state && data.mRelativecatalogs.size() > 0) {
            this.setVisibility(VISIBLE);
            mRootView.setVisibility(VISIBLE);
            mCatalogItem = data.mCatalogItem;
            mAdapter.setData(data.mRelativecatalogs);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDataFault(Object data) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ArticleItem article = (ArticleItem) mAdapter.getItem(position);
        CatalogItem catalog = mCatalogItem.get(position);
        //todo 校验item的参数是否过大
        NewsItemClickUtils.OpenItemNewsHandle(getContext(), catalog.getCatalogStyle(), catalog, new CatalogItem());
    }

    @Override
    public void onItemClick(int index, boolean isFromAdaptor, BaseRecyclerAdapter adapter) {
        ArticleItem article = (ArticleItem) mAdapter.getItem(index);
        CatalogItem catalog = mCatalogItem.get(index);
        //todo 校验item的参数是否过大
        NewsItemClickUtils.OpenItemNewsHandle(getContext(), catalog.getCatalogStyle(), catalog, new CatalogItem());
    }

    private class RecommendComponentAdapter extends BaseRecyclerAdapter<ArticleItem> {
        public RecommendComponentAdapter(Context context) {
            super(context);
        }

        public RecommendComponentAdapter(List data, Context context) {
            super(data, context);
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(getContext()).inflate(R.layout.related_column_item, parent, false);
            return new RecommendComponentViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            if (holder instanceof RecommendComponentViewHolder) {
                RecommendComponentViewHolder recommendComponentViewHolder = (RecommendComponentViewHolder) holder;
                recommendComponentViewHolder.itemView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (itemClickListener != null) {
                            itemClickListener.onItemClick(position, true, RecommendComponentAdapter.this);
                        }
                    }
                });
                recommendComponentViewHolder.onBindViewHolder(getData().get(position));
            }

        }
    }

    private class RecommendComponentViewHolder extends RecyclerView.ViewHolder {

        public RecommendComponentViewHolder(View itemView) {
            super(itemView);
        }

        public void onBindViewHolder(ArticleItem articleItem) {
            ImageView im = itemView.findViewById(R.id.relatedColumnImage);
            TextView tv = itemView.findViewById(R.id.relatedColumnTitle);
            CardView cardView = itemView.findViewById(R.id.card_view);
            //if (!AppFactoryGlobalConfig.getAppServerConfigInfo(itemView.getContext()).isRound()) {
                cardView.setRadius(getResources().getDimensionPixelSize(R.dimen.dimen2));
           // }
            Drawable defaultDrawable = AppFactoryGlobalConfig.getDefaultImageLoadDrawable();
           // GlideUtils.loadUrl(articleItem.getLogo(), im, null, defaultDrawable, false, false);
            FunKt.load(im,articleItem.getLogo(),defaultDrawable);
//            im.defaultBackGroundResId= R.drawable.defaultloading_rectbg;//DefaultBgUtil.getDefaultImageBg(getContext());
//            im.load(articleItem.getLogo());//,AppFactoryGlobalConfig.getAppServerConfigInfo(getContext()).isRound());
            tv.setText(articleItem.getTitle());
            PayTipsUtilsKt.payTips(tv, itemView.getContext().getResources().getText(R.string.pay_a_pei), articleItem, itemView.getResources().getDimensionPixelSize(R.dimen.pay_label_size), itemView.getResources().getDimensionPixelSize(R.dimen.pay_label_bgradius));
        }
    }
    public class RelatedAdapter extends BaseAdapter {
        List<ArticleItem> articleList;
        Context mContext;

        public RelatedAdapter(Context mContext) {
            this.articleList = new ArrayList<>();
            this.mContext = mContext;
        }

        @Override
        public int getCount() {
            return articleList.size();
        }

        public void setArticleList(List<ArticleItem> articleList) {
            this.articleList.clear();
            this.articleList.addAll(articleList);
            notifyDataSetChanged();
        }

        @Override
        public Object getItem(int position) {
            return articleList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null)
            {
                convertView = View.inflate(mContext, R.layout.related_column_item, null);
            }

            NetImageViewX im = convertView.findViewById(R.id.relatedColumnImage);
            TextView tv = convertView.findViewById(R.id.relatedColumnTitle);
            im.defaultBackGroundResId= R.drawable.defaultloading_rectbg;//DefaultBgUtil.getDefaultImageBg(getContext());
            im.load(articleList.get(position).getLogo());//,AppFactoryGlobalConfig.getAppServerConfigInfo(getContext()).isRound());
//            tv.setText(articleList.get(position).getTitle());
            PayTipsUtilsKt.payTips(tv,mContext.getResources().getText(R.string.pay_a_pei),articleList.get(position),mContext.getResources().getDimensionPixelSize(R.dimen.pay_label_size),mContext.getResources().getDimensionPixelSize(R.dimen.pay_label_bgradius));
            return convertView;
        }
    }
}